<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"
      xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<head>
  <title>Ubiquity Web Search Commands for Japanese, nora - goo-weather</title>
</head>
<body>
<p>This xhtml source contains commands for searching the Web.  Its
corresponding feed is <a href="ubiquity-commands-ja-nora-goo-weather.html">here</a>.</p>


<div id="gooweather" style="display : none;">
<div style="font-size:80%">郵便番号<b>${query}</b>でgoo天気を検索:</div>
  <div style="clear: both;font-size:90%;">
    {if item.title}
        <div style="border:1px solid #888888; -moz-border-radius:4px;background:#777777;margin:0px;padding:0px;">
            <b>${item.title}</b> ${item.today}
        </div>
    {/if}
    {if item.todayTable}
        <div>
          ${item.todayTable}
          ${item.tomorrowTable}
        </div>
    {elseif item.weeklyTable}
        <div>
          ${item.weeklyTitle}
          ${item.weeklyTable}
        </div>
    {else}
        <P>検索条件に該当する地域はありませんでした。</P>
    {/if}
  </div>
</div>



<script class="commands"><![CDATA[


CmdUtils.CreateCommand({
         
    name: "goo-weather",
    description: "goo天気",
    takes: {"ZIP": noun_arb_text},
    icon: "http://weather.goo.ne.jp/favicon.ico",
    homepage: "http://code.google.com/p/ubiquity-commands-ja-nora/",
    author: {name: "hokunarin", email: "hokunarin@gmail.com"},
    license: "MPL",

    preview: function( previewBlock, directObject ) {

        var searchText = jQuery.trim(directObject.text);
        if(searchText.length < 7) {
            var previewStr = "<small><b>ピンポイント天気検索  − goo 天気</b><br/>7桁の郵便番号を入力します。</small>";
            previewBlock.innerHTML = previewStr;
            return;
        }

        var previewTemplate = "<b>${query}</b> を goo 天気で検索します...";
        var previewData = {query: searchText};
        previewBlock.innerHTML = CmdUtils.renderTemplate(previewTemplate, previewData);
        
        var queryUrl = "http://weather.goo.ne.jp/freewordsearcher.html?MT={QUERY}&id=top";
        queryUrl = queryUrl.replace(/%s|{QUERY}/g, escape(Utils.convertFromUnicode("euc-jp", searchText)));
        
        CmdUtils.previewAjax(previewBlock, {
          type: "GET",
          url: queryUrl,
          dataType: "html",
          
          error: function() {
            previewBlock.innerHTML = "Error searching Goo Weather.";
          },
          
          success: function(responseData) {
            
            responseData = jQuery(responseData);
            
            var baseHref = "http://weather.goo.ne.jp/";
            
            var title = responseData.find(".ch04:first").text();
            var today = responseData.find(".fs14:first").text();
            
            //Utils.reportInfo(title + today);
            
            /*
            responseData.find("table img").attr("src", function(){
                Utils.reportInfo(this.src);
                return baseHref + this;
            });
            */
            responseData.find("table img").each( function(){
                //Utils.reportInfo(jQuery(this).attr("src"));
                elem = jQuery(this);
                elem.attr("src", baseHref + jQuery(this).attr("src"));
                return elem;
            });
            
            //today
            var todayTable = responseData.find("table.t01:first");
            
            //format
            todayTable.find("table,td,th").css({ "border-spacing":"0px", 
                                                 "border": "1px solid #444466", 
                                                 "border-collaps":"collaps", 
                                                 "font-size":"85%",
                                                 "-moz-border-radius":"2px",
                                                 "background":"#333333",
                                                 "margin":"0px",
                                                 "padding":"0px",
                                                 "width":"90%" });
            todayTable.find("th").css({ "font-size" : "75%",
                                        "background":"#555555"});
            todayTable.find("font[color]").removeAttr("color");
            
            todayTable = todayTable.outerHTML();
            
            //Utils.reportInfo(todayTable);
            
            //tomorrow
            var tomorrowTable = responseData.find("table.t01:eq(1)");
            
            //format
            tomorrowTable.find("table,th,td").css({ "border-spacing":"0px", 
                                                    "border": "1px solid #444466", 
                                                    "border-collaps":"collaps", 
                                                    "font-size":"85%",
                                                    "-moz-border-radius":"2px",
                                                    "background":"#333333",
                                                    "margin":"0px",
                                                    "padding":"0px",
                                                    "width":"90%" });
            tomorrowTable.find("th").css({"font-size" : "75%",
                                            "background":"#555555"});
            tomorrowTable.find("font[color]").removeAttr("color");
            
            tomorrowTable = tomorrowTable.outerHTML();
            
            
            //weekly
            var weeklyTable, weeklyTitle;
            /*
            weeklyTable = responseData.find("table.t03");
            weeklyTable.find("table,th,td").css({ "border-spacing":"0px", 
                                                  "border":"1px solid #444466", 
                                                  "font-size" : "85%",
                                                  "border-collaps" : "collaps"});
            weeklyTable.find("th").css({"font-size" : "75%"});
            weeklyTable = weeklyTable.outerHTML();
            
            weeklyTitle = responseData.find(".ch04:eq(1)").text();
            */
            
            var item = {title : title,
                        today : today,
                        todayTable : todayTable,
                        tomorrowTable : tomorrowTable,
                        weeklyTable : weeklyTable,
                        weeklyTitle : weeklyTitle};
            
            var previewData = {item:item, query:searchText};
            
            previewBlock.innerHTML = CmdUtils.renderTemplate(
              jQuery("#gooweather", feed.dom).html(),
              previewData);
          },
        
          beforeSend: function(xhr){
            xhr.overrideMimeType("text/html;charset=EUC-JP");
          }
          
        });

    },

    execute: function( directObject ) {
        Utils.openUrlInBrowser( "http://weather.goo.ne.jp/freewordsearcher.html?MT={QUERY}&id=top".replace(/¥s|{QUERY}/g, escape( Utils.convertFromUnicode("euc-jp", directObject.text) ) ) );
    }
});

jQuery.fn.outerHTML = function(s) {
    return (s) ? this.before(s).remove() : jQuery("<p>").append(this.eq(0).clone()).html();
}

]]></script>
</body>
</html>
